//
//  SYLoginViewController.m
//  BSHOUYIFANG
//
//  Created by lanou3g on 15/8/14.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYLoginViewController.h"
#import "Header.h"
#import "SYRegistViewController.h"
#import "SYPickUpViewController.h"
#import "SYProtocolViewController.h"
#import "SYUserViewController.h"
#import "SYFileHandle.h"
#import "SYMyFmdb.h"
@interface SYLoginViewController ()
@property(nonatomic,strong)UILabel *labelAccessToken;
@property(nonatomic,strong)UILabel *labelTitle;
@end

@implementation SYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.navigationItem.title =@"登录";
    _longinView = [[SYLoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    [self.view addSubview:_longinView];

        //登录按钮添加响应方法
     [_longinView.longinButton addTarget:self action:@selector(longinButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //协议按钮添加响应方法
     [_longinView.protocolButton addTarget:self action:@selector(protocolButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //登录页面的注册按钮添加响应方法
    [_longinView.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //登录页面的找回密码添加响应方法
    [_longinView.seekPassButton addTarget:self action:@selector(seekPassButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        //登录页面的使用qq登陆添加响应方法
//    [_longinView.qqLoginButton addTarget:self action:@selector(qqLoginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //__weak SYLoginViewController *tempVC = self;

           // Do any additional setup after loading the view.
}



# pragma mark ------控制方法-----
//点击登录按钮，验证用户登录
- (void)longinButtonAction:(UIButton *)sender{
    //从沙盒中取出手机号和密码
    SYUser *user = [[SYMyFmdb shareInstance] getUserInfophoneNumber:_longinView.phoneNumberTF.text];
    NSString *phoneNumber = user.phoneNumber;
    NSString *password = user.password;
      [[SYFileHandle shareInstance]setUserForAll:user];
    NSLog(@"%@",password);
    NSLog(@"%@",phoneNumber);
    //验证登录
    if ([_longinView.phoneNumberTF.text isEqualToString:phoneNumber]&&[_longinView.passWordTF.text isEqualToString:password]) {
        [[SYFileHandle shareInstance] setloginState:YES];
        if (self.loginAppear != nil) {
             _loginAppear();
        }
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        if ([_longinView.phoneNumberTF.text isEqualToString:@"admin"]&&[_longinView.passWordTF.text isEqualToString:@"8888"]) {
            [[SYFileHandle shareInstance] setloginState:YES];
            if (self.loginAppear != nil) {
                _loginAppear();
            }
            [self.navigationController popToRootViewControllerAnimated:YES];

        }else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名不存在或者密码错误" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
             [alertView show];
        }
    }
}

- (void)registerButtonAction:(UIButton *)sender{
    SYRegistViewController *registVC =[[SYRegistViewController alloc]init];
    [self.navigationController pushViewController:registVC animated:YES];
}

- (void)seekPassButtonAction:(UIButton *)sender{
    SYPickUpViewController *pickupVC = [[SYPickUpViewController alloc]init];
    [self.navigationController pushViewController:pickupVC animated:YES];
}

- (void)protocolButtonAction:(UIButton *)sender{
    SYProtocolViewController *protocolVC = [[SYProtocolViewController alloc] init];
    [self.navigationController pushViewController:protocolVC animated:YES];
}

//- (void)qqLoginButtonAction:(UIButton *)sender{
//    _tencentoauth = [[TencentOAuth alloc]initWithAppId:@"1033726349" andDelegate:self];
//   // _tencentoauth.redirectURI = @"www.qq.com";
//    [self qq];
//}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}



/**
 *  qq登陆
 */

/**
 * 登录失败后的回调
 * \param cancelled 代表用户是否主动退出登录
 */
- (void)tencentDidNotLogin:(BOOL)cancelled
{
    
}
/**
 * 登录时网络有问题的回调
 */
- (void)tencentDidNotNetWork
{
    NSLog(@"网络连接失败");
}
- (void)qq
{
//    [_tencentoauth authorize:_permissions inSafari:YES];
}
- (NSArray *)getAuthorizedPermissions:(NSArray *)permissions withExtraParams:(NSDictionary *)extraParams
{
    self.permissions =  [NSArray arrayWithObjects:@"get_user_info", @"get_simple_userinfo", @"add_t", nil];
    return _permissions;
}


//- (void)tencentDidLogin
//{
//     NSLog(@"登陆成功");
//    _labelTitle.text = @"登录完成";
//    
//    if (_tencentoauth.accessToken && 0 != [_tencentoauth.accessToken length])
//    {
//        //  记录登录用户的OpenID、Token以及过期时间
//        _labelAccessToken.text = _tencentoauth.accessToken;
//    }
//    else
//    {
//        _labelAccessToken.text = @"登录不成功 没有获取accesstoken";
//    }
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
